package controller;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import dao.StatusDAO;
import dao.ThongsoDAO;

import pojo.Comment;
import pojo.Status;

import pojo.User;

@Controller

public class TrangThaiController {
	
	@RequestMapping (value = "LietKeTrangThai.do", method = RequestMethod.GET)
	public String lietkeQuangcao(
			HttpServletRequest request)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		StatusDAO DAO= new StatusDAO();
		List<Status> ltt= DAO.getList();
		
				
		

		request.setAttribute("ltt", ltt);		


		request.setAttribute("title", "Liệt kê Trạng thái");

		return "page/BackEnd/LietKeTrangThai";
	}	
	
	@RequestMapping(value ="SuaTrangThai.do", method = RequestMethod.GET)
	public String suaQC(Model model,
			HttpServletRequest request,
			@RequestParam(value="idtt", required = false)String idtt
			)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		StatusDAO DAO= new StatusDAO();
		int id= Integer.parseInt(idtt);
		Status tt= DAO.getStatusByID(id);
		model.addAttribute("tt", tt);
		String link = "SuaTrangThai.do?idtt="+idtt;			
		model.addAttribute("link", link);
		return "page/BackEnd/SuaTrangThai";
	}
	
	@RequestMapping(value ="SuaTrangThai.do", method = RequestMethod.POST)
	public String suaQCPost(
			@RequestParam(value="trangthai", required = false)String trangthai,
			@RequestParam(value="idtt", required = false)String idtt,
			HttpServletRequest request,
			Model model)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		String title="";
		String output="";
		StatusDAO dao = new StatusDAO();
		boolean flag=dao.capnhatTrangThai(idtt, trangthai);
		if(flag==true)
		{
				return  "redirect:LietKeTrangThai.do";
		} 
		else
		{

	        title="Cập nhật thông số thất bại ";
	        output= "Thao tác cập nhật thông số thất bại";
			String link="LietKeTrangThai.do";
			model.addAttribute("title",title);
			model.addAttribute("output", output);
			model.addAttribute("link", link);
			return  "template/ketqua";
		}
		


	}
}


